//////////////////////////////////////////
//  File       	: client.h              //
//  Written by	: ludmiloff@gmail.com	//
//  Copyright  	: GPL                   //
//////////////////////////////////////////

#ifndef __CLIENT_H
#define __CLIENT_H

#include "defs.h"

#define CLIENT_EVENT_MASK \
	ColormapChangeMask|\
	PropertyChangeMask|\
	StructureNotifyMask|\
	FocusChangeMask

#define DECOR_EVENT_MASK \
	ButtonPressMask|ButtonReleaseMask|\
	KeymapStateMask|\
	ButtonMotionMask|PointerMotionMask|\
	EnterWindowMask|LeaveWindowMask|FocusChangeMask|\
	ExposureMask|StructureNotifyMask|SubstructureRedirectMask|SubstructureNotifyMask

#define FLAG_HAS_BORDER			(1L<<0)
#define FLAG_HAS_MENU			(1L<<1)
#define FLAG_HAS_MAXIMIZE		(1L<<2)
#define FLAG_HAS_CLOSE			(1L<<3)
#define FLAG_HAS_HIDE			(1L<<4)
#define FLAG_HAS_MOVE			(1L<<5)
#define FLAG_HAS_RESIZE			(1L<<6)
#define FLAG_HAS_STICK			(1L<<7)

#define FLAG_FOCUS				(1L<<8)
#define FLAG_IS_RESIZABLE		(1L<<9)
#define FLAG_MAP_PENDING		(1L<<10)
#define FLAG_VISIBLE			(1L<<11)
#define FLAG_MANAGED			(1L<<12)
#define FLAG_SESSION_MANAGED	(1L<<13)

#define FLAG_FIRST_MAP			(1L<<19)

#define FLAG_NEEDS_REDRAW		(1L<<22)

#define FLAG_INITIAL	FLAG_HAS_BORDER | \
						FLAG_HAS_MENU | \
						FLAG_HAS_MAXIMIZE | \
						FLAG_HAS_STICK | \
						FLAG_HAS_HIDE | \
						FLAG_HAS_CLOSE | \
						FLAG_HAS_MOVE | \
						FLAG_HAS_RESIZE | \
						FLAG_FIRST_MAP | \
						FLAG_NEEDS_REDRAW

class Client;
class Titlebar;
class GenericBar;
class Dockbar;
class Desktop;
class MyScreen;

typedef QHash<int, Client *> ClientList;

class Client : public QWidget
{
	Q_OBJECT

public:
	Client(Window w, QWidget *parent = 0);
	~Client();

	bool init();
	void createDecor();
	void setDecorState(bool);

	// X11 management routines
    void getWMHints();
	void getWMNormalHints(bool update=false);
	void getMotifHints();
	void getWMProtocols();

	void getWindowName();
	void getWindowHostName();

	void getTransientFor(Window root, Window *transient);
	bool isTransient();
	Client *getTransient();

	void getClientLeader();

	// placement
	void initPosition();
	void moveAt(const QPoint &p);

	void getAppName();
	void getColormaps(void);
	void setColormaps(Colormap cmap);
	void setClientState(int state);
	void fixSizes();
	void resizeRequest(int cx, int cy, int cw, int ch);
	void setChildFocus(Window subwindow, long timestamp);
	void delayedGrab(int);

	// User/system management routines
public slots:
	void iconify();
	void iconifyFast();
	void maximize();
	void map();
	void mapFast();
	void unmap();
	void removeFromDock();
	void destroyClient();
	// Window movement & resize
	void titlebarMousePress(QMouseEvent *);
	void titlebarMove(QMouseEvent *);
	void bottombarMousePress(QMouseEvent *);
	void bottombarMove(QMouseEvent *);
	void bottombarMouseRelease();

	void doGrab();
	void setDblMinimizeClick(bool);

public:
	bool initialized;
	MyScreen *screen_info;
	QString clientName;			// client title/iconic name
	QString hostName;			// host machine name
	Window clientId;			// client x11 window id
	Window transientFor;
	QList <Window> transients;
	Window clientLeader;
	Window groupLeader;
	QPixmap appIcon;			// client window icon
	int clientState;

	bool protDelete;			// client has WM_DELETE protocol
	bool protTakeFocus;			// client has WM_TAKE_FOCUS protocol
	bool wantFocus;				// does this application rely on the window manager to get keyboard input?
	bool Urgency;

	int windowRole;				// window role (Normal, Dialog, Splash, ...)

	Colormap colorMap;			// client colormap
	XSizeHints *xsize;
	XWMHints *xwmhints;
	XClassHint aclass;

	long wmnflags;				// XWMNormalHints
	int gravity;				// XWMNormalHints
	unsigned long flags;
	unsigned long wm_flags;
	unsigned long win_flags;
	unsigned long win_hints;
	unsigned long win_state;
	unsigned long win_layer;

	int base_w, base_h;			// XWMNormalHints
	int inc_w, inc_h;			// XWMNormalHints
	int max_w, max_h;			// XWMNormalHints
	int min_w, min_h;			// XWMNormalHints

	int client_x, client_y;
	int client_w, client_h;     // client geometry
	int border_w;

	QString	appName;			// Application class name
	QString icnName;			// WM_ICON_NAME

	// Decorations widgets
	QGridLayout *layout;
	Titlebar *titlebar;
	GenericBar *bottombar;

	int m_px, m_py, m_pw, m_ph;             // maximize state parent (qt) geometry
	int n_px, n_py, n_pw, n_ph;             // normal state parent (qt) geometry
	bool maximized;

	bool keyboardGrabbed;
	bool dblClickMinimize;

	// internals
	int tbHeight;				// Topbar height
	QPoint mousepos;
	bool resizeState;
};

#endif
